package com.easyban.controller;

import java.util.List;

import com.easyban.Dto.SessionWebUserDto;
import com.easyban.annotation.GlobalInterceptor;
import com.easyban.annotation.VerifyParam;
import com.easyban.entity.query.FileShareQuery;
import com.easyban.entity.po.FileShare;
import com.easyban.entity.vo.ResponseVO;
import com.easyban.service.FileShareService;
import com.easyban.utils.RedisUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 分享信息 Controller
 */
@RestController("fileShareController")
@RequestMapping("/share")
public class FileShareController extends ABaseController{

	@Resource
	private FileShareService fileShareService;
	@Resource
	private RedisUtils redisUtils;
	@RequestMapping("/loadShareList")
	@GlobalInterceptor(checkParams = true)
	public ResponseVO loadShareList(HttpSession httpSession, FileShareQuery query){
		SessionWebUserDto sessionV = getSessionV(httpSession);
		query.setOrderBy("share_time desc");
		query.setUserId(sessionV.getUserId());
		query.setQueryFileName(true);
		return getSuccessResponseVO(fileShareService.findListByPage(query));
	}
	@RequestMapping("/shareFile")
	@GlobalInterceptor(checkParams = true)
	public ResponseVO shareFile(HttpSession session,
								@VerifyParam(required = true) String fileId,
								@VerifyParam(required = true) Integer validType,
								String code) {
		SessionWebUserDto userDto = getSessionV(session);
		FileShare share = new FileShare();
		share.setFileId(fileId);
		share.setValidType(validType);
		share.setCode(code);
		share.setUserId(userDto.getUserId());
		fileShareService.saveShare(share);
		return getSuccessResponseVO(share);
	}

	@RequestMapping("/cancelShare")
	@GlobalInterceptor(checkParams = true)
	public ResponseVO cancelShare(HttpSession session, @VerifyParam(required = true) String shareIds) {
		SessionWebUserDto userDto = getSessionV(session);
		fileShareService.deleteFileShareBatch(shareIds.split(","), userDto.getUserId());
		/*清理redis缓存*/
		redisUtils.removeShareValue(shareIds);
		return getSuccessResponseVO(null);
	}
	/**
	 * 新增
	 */
	@RequestMapping("/add")
	public ResponseVO add(FileShare bean) {
		fileShareService.add(bean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 批量新增
	 */
	@RequestMapping("/addBatch")
	public ResponseVO addBatch(@RequestBody List<FileShare> listBean) {
		fileShareService.addBatch(listBean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 批量新增/修改
	 */
	@RequestMapping("/addOrUpdateBatch")
	public ResponseVO addOrUpdateBatch(@RequestBody List<FileShare> listBean) {
		fileShareService.addBatch(listBean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 根据ShareId查询对象
	 */
	@RequestMapping("/getFileShareByShareId")
	public ResponseVO getFileShareByShareId(String shareId) {
		return getSuccessResponseVO(fileShareService.getFileShareByShareId(shareId));
	}

	/**
	 * 根据ShareId修改对象
	 */
	@RequestMapping("/updateFileShareByShareId")
	public ResponseVO updateFileShareByShareId(FileShare bean,String shareId) {
		fileShareService.updateFileShareByShareId(bean,shareId);
		return getSuccessResponseVO(null);
	}

	/**
	 * 根据ShareId删除
	 */
	@RequestMapping("/deleteFileShareByShareId")
	public ResponseVO deleteFileShareByShareId(String shareId) {
		fileShareService.deleteFileShareByShareId(shareId);
		return getSuccessResponseVO(null);
	}
}